<template>
  <DataList ref="datalist" :cate="cate" :excelHeader="excelHeader" skipExcelNumber="2" :cols="cols" :name="name" />
</template>
<script>
import DataList from "@/components/DataList";
import { getUser } from "@/utils/auth";
import linq from "linq";
import {get开设专业} from '@/api/开设专业.js'
import {get机构设置,search机构设置} from "@/api/机构设置"
import {findComponent} from "@/utils/ui";
import { addKszy_new, deleteKszy_new, updateKszy_new, getKszy_new, BulkAddKszy_new } from "@/api/补充2020_2021学年开设专业.js";
export default {
  components: {
    DataList
  },
  data() {
    return {
      cate: "A11-5 2020-2021学年开设专业",
      name: '11.5 补充2020-2021学年开设专业',
       excelHeader:{B3:"专业所属系部代码",C3:"专业所属系部名称",D3:"专业代码",E3:"专业名称（全称）",F3:"专业方向代码",G3:"专业方向名称（全称）",H3:"批准设置日期（年月）",I3:"首次招生日期（年月）",J3:"修业年限（年）",K3:"重点专业",L3:"特色专业",M3:"是否现代学徒制试点专业",N3:"是否国际合作专业",O3:"是否有上届毕业生"},
      cols: [
        {label:"专业所属系部代码",prop:"BMBH"},
        {label:"专业所属系部",prop:"BMMC"},
        {label:"专业代码",prop:"ZYDM"},
        {label:"专业名称(全称)",prop:"ZYMC"},
        {label:"专业方向代码",prop:"ZYFXDM"},
        {label:"专业方向名称(全称)",prop:"ZYFXMC"},
        {label:"批准设置日期(年月)",prop:"JLNY"},
        {label:"首次招生日期(年月)116",prop:"QSXQ"},
        {label:"修业年限(年)",prop:"XZ"},
        {label:"新生总人数",prop:"ZXSS"},
        { label: "生源类型(人)", prop: "生源类型" ,data:[
            {label:'普通高中起点',prop:'SYJG_PTGZ'},
            { label: "中职起点", prop: "中职起点" ,data:[
              {label:'合计',prop:'SYJG_ZZQD_HJ'},
              {label:'其中：五年制后二年',prop:'SYJG_ZZQD_HEN'}
            ]},
            { label: "其他", prop: "SYJG_QT" },
        ]},
        { label: "重点专业117", prop: "XZ_ZDZY" },
        { label: "特色专业118", prop: "XZ_TSZY" },
        { label: "是否现代学徒制试点专业", prop: "XZ_SFXTZ" },
        { label: "是否国际合作专业", prop: "XZ_SFGJHZZY" },
        { label: "班级总数(个)", prop: "BJZS" },
        { label: "订单(定向)培养情况", prop: "订单定向培养情况" ,data:[
          { label: "班级数(个)", prop: "DDPYBJS" },
          { label: "学生数(人)", prop: "DDPYXSS" }
        ]},
        { label: "是否有上届毕业生119", prop: "XZ_SFYSJBYS" },
        { label: "检测列：专业是否重复？", prop: "检测列_专业是否重复" }
      ]
    };
  },
  methods: {
    getSummaries(list){
      //
        const sums = [];
         this.cols.forEach((column, index) => {
            sums[index]="";
        });
        sums[0]="合计:"+linq.from(list).where("$.BMMC").count();
        sums[4]= "通过:"+linq.from(list).where("$.审核状态=='通过'").count();
        sums[4] += ",待审核:"+linq.from(list).where("$.审核状态=='待审核'").count();
        sums[6]= "系部数:"+linq.from(list).distinct("$.BMMC").count();
        sums[7]= "专业数:"+linq.from(list).distinct("$.ZYMC").count();
        sums[9]= "方向数:"+linq.from(list).distinct("$.ZYFXMC").count();
       return sums;
      //
    },
    check(array,list) {
       return new Promise(async (resolve,reject)=>{
         try{
       var duplicates = linq
            .from(array)
            .where("!$.error")
            .groupBy("$.BMMC+','+$.ZYDM+','+$.ZYFXDM")
            .where("$.count()>1")
            .toArray();
          duplicates.forEach(e => {
            linq
              .from(array)
              .where(t => t.BMMC+','+t.ZYDM + "," + t.ZYFXDM == e.key())
              .forEach(
                t => (t.error = "录入数据存在相同的系部名称,专业方向代码与专业代码组合")
              );
          });
           
          //是否已经存在数据
          var err = linq
            .from(array)
            .where("!$.error")
            .select("$.ZYDM+','+$.ZYFXDM")
            .intersect(
              linq
                .from(list)
                .select("$.ZYDM+','+$.ZYFXDM")
                .toArray()
            )
            .toArray();
          
          linq
            .from(array)
            .where(e => err.indexOf(e.BMMC+','+e.ZYDM + "," + e.ZYFXDM) !== -1)
            .forEach(
              e =>
                (e.error = e.error
                  ? e.error + ";"
                  : "" + "数据库中已经存在相同的系部名称,专业方向代码与专业代码组合")
            );
            resolve();
          }catch(e){
            reject(e);
          }
     }) 
    },
   async onDlgCreated(model) {
     var user = getUser()
    var schema = require("../../../../static/profession.json");
    var mc = findComponent(this,"ZYMC");
    var cc = schema.map(item => {
    return { label: item.专业名称, value: item.专业名称 };
    }); 

    mc.$attrs.items = cc;
   
    
    var dd = findComponent(this,"BMMC");
      var 专业 = linq.from(this.$root.机构设置).distinct("$.BMMC").toArray();
      dd.$attrs.items = 专业.map(item => {
        return { label: item.BMMC, value: item.BMMC};
      });
    },
    onChange(model, field, value) {
//
console.log(value)
    if(field == "ZYMC" ){
    var schema = require("../../../../static/profession.json");
   console.log(schema)
    var item = schema.find(item => {
    return item.专业名称 == value;
    });
    console.log(item)
    model.ZYDM = item.专业代码
    model.ZYFXDM = item.专业代码
    model.ZYDM = item.专业代码
    model.ZYFXMC = item.专业名称
}
//
},
    async fetchData() {
      var user = getUser();
      var datas = await getKszy_new(user.XXID,user.CJPC);
      return datas;
    },
    async  bulkAdd(array){
       await  BulkAddKszy_new({Kszy_new:array});
  },
   add_check(model,list){
      //
          //是否已经存在数据
          var first =  linq.from(list).firstOrDefault(e=>e.ZYFXDM ==  model.ZYFXDM);
          
          if(first)
          return  "已经存在对应专业"
      //
    },
    onadd(model) {
      //
      var temp = JSON.parse(JSON.stringify(model))
      return new Promise(async (resolve,reject)=>{
           
       try{
        
          console.log(JSON.stringify(temp));
          var model = await addKszy_new(temp);
          resolve(model);
      }catch(e){
        reject(e);
      }
      })
      
    },
    onupdate(model) {
      //
      var temp = JSON.parse(JSON.stringify(model))
      return new Promise(async (resolve,reject)=>{
           
          console.log(JSON.stringify(temp));
          var model = await updateKszy_new(temp);
          resolve(model);
      })
      //
    },
    ondelete(model) {
       var temp = JSON.parse(JSON.stringify(model))
      return new Promise(async (resolve,reject)=>{
        
         await deleteKszy_new(temp);
          resolve();
      })
    }
  }
};
</script>
<style>
</style>